package com.example.czy.aspect;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.example.czy.annotation.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * 日志切面
 *
 * @author czy
 * @date 2024-01-01 14:13:52
 **/
@Aspect
@Slf4j
@Component
public class LogAspect {
    public static final String  USER_AGENT = "User-Agent";

    @Pointcut("execution(public * com.example.czy.controller.*.*(..))")
    public void pointcut() {
    }

    /**
     * 执行时通知
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object obj = joinPoint.proceed();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SysLog sysLog = method.getAnnotation(SysLog.class);
        if (null != sysLog) {
            log.info("日志名称：{}", sysLog.name());
            log.info("前置环绕通知 - 方法：{} ", joinPoint.getSignature().getName());
            Object[] args = joinPoint.getArgs();
            if (!ObjectUtils.isEmpty(args)) {
                log.info("参数1:{}", BeanUtil.beanToMap(args[0]));
            }

            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = servletRequestAttributes.getRequest();
            UserAgent userAgent = UserAgentUtil.parse(request.getHeader(USER_AGENT));
            String clientIP = ServletUtil.getClientIP(request);
            log.info("客户机系统信息：{}", BeanUtil.beanToMap(userAgent));
            log.info("客户机IP：{}", clientIP);
            log.info("后置环绕通知 - 方法：{}", joinPoint.getSignature().getName());

        }

        return obj;
    }

    /**
     * 前置通知
     *
     * @param joinPoint
     */
    @Before("pointcut()")
    public void before(JoinPoint joinPoint) {
        // log.info("前置通知 - 方法：{}", joinPoint.getSignature().getName());
    }

    /**
     * 后置通知
     *
     * @param joinPoint
     */

    @After("pointcut()")
    public void after(JoinPoint joinPoint) {
        // log.info("后置通知 - 方法：{}", joinPoint.getSignature().getName());
    }


    /**
     * 返回结果通知
     *
     * @param joinPoint
     * @param result
     */
    @AfterReturning(returning = "result", pointcut = "pointcut()")
    public void afterReturning(JoinPoint joinPoint, Object result) {
        // log.info("返回通知 - 方法：{}，返回值：{}", joinPoint, result);
    }


    /**
     * 异常通知
     *
     * @param joinPoint
     * @param ex
     */
    @AfterThrowing(throwing = "ex", pointcut = "pointcut()")
    public void afterThrowing(JoinPoint joinPoint, Exception ex) {
        // log.error("异常通知 - 方法：{}，异常：{}", joinPoint.getSignature().getName(), ex.getMessage());
    }
}
